import { message } from 'antd';
export default {
  namespace: 'ccc',

  state: {
    light1Params: [], // 灯管1的信息
    light2Params: [], // 灯管2的信息
    light3Params: [], // 灯管3的信息
    light4Params: [], // 灯管4的信息
    cutterImageModalVisible: false, // 铡刀的图片展示是否显示
    cutterCamSrc: null, // 铡刀那里标定的时候图片展示的内容
    hintInfo: "无消息", // 铡刀标定的时候的提示消息
  },

  // 和后台交互
  effects: {
    *handleCutterCameraCalMsg({ payload }, { call, put }) {
      const { msg } = payload;
      if (msg.val === 'updateLightParams') {
        //更新界面上显示的参数列表
        yield put({
          type: 'getLightParams',
        });
      } else if (msg.val == "updateCutterCalImage") {
        // 更新显示的图片内容
        yield put({
          type: 'updateCutterCalImage',
          payload: [true, msg.cutterCalImage, msg.hintInfo],
        });
      }
    },
    *getLightParams({ payload }, { call, put }) {
      const lightParams = yield call(window.eel.run_py, 'cutterCamCal', 'web_request_get_light_params', []);
      yield put({
        type: 'updateLightParams',
        payload: lightParams,
      });
    },
    *checkLightPos({ payload }, { call, put }) {
      const lightParams = yield call(window.eel.run_py, 'cutterCamCal', 'web_request_check_light_pos', [payload]);
    },
    *saveLightParams({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'cutterCamCal', 'web_request_save_light_params', [payload]);
      if (response !== "ok") {
        message.error("保存灯管参数失败,原因:" + response, 5);
      } else {
        message.success("保存灯管参数成功!", 3);
      }
      yield put({
        type: 'getLightParams'
      });
    },
    *calCutterCam1({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'cutterCamCal', 'web_request_cal_cutter_cam1', [payload]);
      if (response !== "ok") {
        message.error("1号相机标定失败: " + response, 5)
      }
      yield put({
        type: 'getLightParams'
      });
    },

    *calCutterCam2({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'cutterCamCal', 'web_request_cal_cutter_cam2', [payload]);
      if (response !== "ok") {
        message.error("2号相机标定失败: " + response, 5)
      }
      yield put({
        type: 'getLightParams'
      });
    },

    *verifyCutterCam1Cal({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'cutterCamCal', 'web_request_verify_cutter_cam1_cal', []);
      if (response !== "ok") {
        message.error("1号相机验证失败: " + response, 5)
      }
    },
    *verifyCutterCam2Cal({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'cutterCamCal', 'web_request_verify_cutter_cam2_cal', []);
      if (response !== "ok") {
        message.error("2号相机验证失败: " + response, 5)
      }
    },
    *verifyCutterCamTotal({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'cutterCamCal', 'web_request_verify_cutter_cam_total', []);
      if (response !== "ok") {
        message.error("总板长验证失败: " + response, 5)
      }
    },

    *openCutterCamCalDir({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'cutterCamCal', 'web_request_open_cutter_cam_cal_dir', []);
      if (response !== "ok") {
        message.error("目录打开失败: " + response, 5)
      } else {
        message.success("目录打开成功!",3)
      }
    },

    *cutterDown({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'cutterCamCal', 'web_request_cutter_down', [payload]);
      if (response !== "ok") {
        message.error("铡刀下降指令发送失败: " + response, 5)
      } else {
        message.success("铡刀下降指令发送成功!",3)
      }
    },

    *deleteTotalCutterCalImage({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'cutterCamCal', 'web_request_delete_total_cutter_cal_image', []);
      if (response !== "ok") {
        message.error("删除失败: " + response, 5)
      } else {
        message.success("删除成功!",3)
      }
    },

    *verifyCutterBeginCheck({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'cutterCamCal', 'web_request_verify_cutter_begin_check', []);
      if (response !== "ok") {
        message.error("验证失败: " + response, 5)
      } else {
      }
    },
    *closeCutDoorAlarm({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'cutterZmc', 'close_door_alarm');
      if (response === 0) {
        message.success("关闭铡刀门报警成功", 3);
      } else {
        message.error("关闭铡刀门报警失败!" + response, 5);
      }
    },
    *openCutDoorAlarm({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'cutterZmc', 'open_door_alarm');
      if (response === 0) {
        message.success("打开铡刀门报警成功", 3);
      } else {
        message.error("打开铡刀门报警失败!" + response, 5);
      }
    },
  },

  // 状态更新
  reducers: {
    updateLightParams(state, action) {
      return {
        ...state,
        light1Params: action.payload[0],
        light2Params: action.payload[1],
        light3Params: action.payload[2],
        light4Params: action.payload[3]
      };
    },

    closeCutterCalImageModal(state, action) {
      return {
        ...state,
        cutterImageModalVisible: false,
        cutterCamSrc: null,
        hintInfo: "无提示消息"
      }
    },

    updateCutterCalImage(state, action) {
      return {
        ...state,
        cutterImageModalVisible: action.payload[0],
        cutterCamSrc: 'data:image/jpeg;base64,' + action.payload[1],
        hintInfo: action.payload[2],
      };
    },
  },
};
